package com.example.administrator.szb.activity.myyewu;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.CJYWXQActivity;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.bean.ResultBean;
import com.example.administrator.szb.bean.YWGLBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.ScreenExpressionUtils;
import com.example.administrator.szb.util.Toasts;
import com.example.zhouwei.library.CustomPopWindow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyYeWuActivity extends MVPBaseActivity {
public static boolean isLoad = false;
    private ListView listView;
    private Button btn_del;
    private TextView bar_title;
    private TextView bar_right;
    private boolean flag = false;//是否是取消
    private MyYeWuAdapter adapter;
    private ArrayList<Integer> integerList1_1 = new ArrayList<>();//一级选中标签(有二级标签如股票质押)
    private ArrayList<Integer> integerList1_2 = new ArrayList<>();//一级选中标签(无二级标签如市值管理)
    private ArrayList<Integer> integerList2 = new ArrayList<>();//二级选中标签
    private ArrayList<Integer> integerList3 = new ArrayList<>();//三级选中标签
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_ye_wu);
        isLoad  =true;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        listView = (ListView) findViewById(R.id.listView);
        btn_del = (Button) findViewById(R.id.btn_del);
        bar_title = (TextView) findViewById(R.id.bar_title);
        bar_right = (TextView) findViewById(R.id.bar_right);
        bar_title.setText("业务管理");
        bar_right.setText("编辑");
        bar_right.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initEvent() {
        btn_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adapter!=null&& !TextUtils.isEmpty(adapter.getSelect())){

                    new AlertDialog.Builder(context)
                            .setTitle("提示")
                            .setMessage("确认删除？")
                            .setNegativeButton("取消",null)
                            .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    doDel(adapter.getSelect());
                                }
                            }).show();



                }
            }
        });
        bar_right.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View v) {
                if (flag){
                    flag = false;
                    btn_del.setVisibility(View.GONE);
                    if (adapter!=null){
                        adapter.setShowCheckBox(false);
                    }
                    bar_right.setText("编辑");
                }else {

                    showPopupwindow(R.layout.view_popupwindow_myzx, R.layout.activity_my_ye_wu);
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        requestData();
    }

    /**
     * 初始化popupwindow弹出框
     */
    public CustomPopWindow popWindow;//弹出框
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void showPopupwindow(int view_layout_id, int rootview_layout_id) {

        View popwindow_view = LayoutInflater.from(this).inflate(view_layout_id, null);
        ((TextView) popwindow_view.findViewById(R.id.view_my_zx_twzx)).setText("修改");
        ((TextView) popwindow_view.findViewById(R.id.view_my_zx_spzx)).setText("删除");
        View rootview = LayoutInflater.from(this).inflate(rootview_layout_id, null);

        //如果不是进度框，就执行底部弹出
        if (view_layout_id != R.layout.view_progressbar) {
            popWindow = new CustomPopWindow.PopupWindowBuilder(this)
                    .setView(popwindow_view)//显示的布局，还可以通过设置一个View
                    .create()//创建PopupWindow
                    .showAtLocation(rootview, Gravity.TOP | Gravity.RIGHT, ScreenExpressionUtils.dip2px(this, 10), ScreenExpressionUtils.dip2px(this, 70));
            handleLogic(popwindow_view, popWindow);//处理popupwindow 中的item点击事件
        }
        //显示PopupWindow

    }


    /**
     * 处理弹出显示内容、点击事件等逻辑
     *
     * @param contentView
     */
    private void handleLogic(View contentView, final CustomPopWindow mCustomPopWindow) {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCustomPopWindow != null) {
                    mCustomPopWindow.dissmiss();
                }
                switch (v.getId()) {
                    case R.id.view_my_zx_twzx:
                        // finish();
                        Intent intent = new Intent(context, YWNOActivity2.class);
                        intent.putExtra("list1_1",integerList1_1);
                        intent.putExtra("list1_2",integerList1_2);
                        intent.putExtra("list2",integerList2);
                        intent.putExtra("list3",integerList3);
                        startActivity(intent);

                        break;
                    case R.id.view_my_zx_spzx:
                        if (list.size()==0){
                            return;
                        }
                        bar_right.setText("取消");
                        flag = true;
                        btn_del.setVisibility(View.VISIBLE);
                        if (adapter!=null){
                            adapter.setShowCheckBox(true);
                        }
                        break;
                }
                //Toasts.show(MingPianActivity.this, showContent, Toast.LENGTH_SHORT);
            }
        };
        contentView.findViewById(R.id.view_my_zx_twzx).setOnClickListener(listener);
        contentView.findViewById(R.id.view_my_zx_spzx).setOnClickListener(listener);

    }
    private List<YWGLBean> list = new ArrayList<>();
    private void requestData() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("users_id", SampleApplicationLike.getUserloginInstance().getId() + "");
        String url =  URLAddress.URLBASE + URLAddress.URL_YWBQGL;
        HttpUtils.post(activity, url, map, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                str = str.replace("{}","[]");
                list.clear();
                list.addAll(JSON.parseArray(str,YWGLBean.class));
                integerList1_1.clear();
                integerList1_2.clear();
                integerList2.clear();
                integerList3.clear();
                for (int i=0;i<list.size();i++){
                    if (list.get(i).getType()==1){
                        integerList1_2.add(list.get(i).getId());
                    }else {
                        integerList1_1.add(list.get(i).getId());
                        for (int j=0;j<list.get(i).getNext().size();j++){
                            if (list.get(i).getNext().get(j).getGenre()==1){
                                integerList2.add(list.get(i).getNext().get(j).getId());
                            }
                            for (int k=0;k<list.get(i).getNext().get(j).getNext().size();k++){
                                if (list.get(i).getNext().get(j).getNext().get(k).getGenre()==1){
                                    integerList3.add(list.get(i).getNext().get(j).getNext().get(k).getId());
                                }
                            }

                        }

                    }
                }
                if (adapter==null){
                    adapter = new MyYeWuAdapter(context,list);
                    adapter.setOnItemClickListener(new MyYeWuAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(int pos) {
                            if (list.get(pos).getNext().size() > 0){
                                Intent intent = new Intent(context,CJYWXQActivity.class);
                                intent.putExtra("data",list.get(pos));
                                /*intent.putExtra("title",list.get(pos).getTitle());
                                intent.putExtra("position",pos);*/
                                startActivity(intent);
                            }
                        }
                    });
                    listView.setAdapter(adapter);
                }else {
                    adapter.notifyDataSetChanged();
                }

                if (list.size() == 0){
                    Intent intent = new Intent(context, YWNOActivity2.class);

                    Toasts.show(context, "快去添加便签吧。。。", Toast.LENGTH_SHORT);
                    intent.putExtra("list1_1",integerList1_1);
                    intent.putExtra("list1_2",integerList1_2);
                    intent.putExtra("list2",integerList2);
                    intent.putExtra("list3",integerList3);
                    startActivity(intent);

                    finish();
                }
            }

            @Override
            public void onFailure(int requestCode, String str) {
                Toasts.show(context,"加载失败");
            }
        });

    }



    private void doDel(String id_info) {
        HashMap<String,String> map = new HashMap<>();
        map.put("code",HttpUtil.getParam());
        map.put("users_id",""+SampleApplicationLike.getUserloginInstance().getId());
        map.put("business_id",id_info);
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, ResultBean.class, URLAddress.URLBASE + URLAddress.URL_DELYWBQ, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                ResultBean resultBean = (ResultBean) response;
                if (resultBean.getResult() == 1){
                    //finish();
                    flag = false;
                    btn_del.setVisibility(View.GONE);
                    if (adapter!=null){
                        adapter.setShowCheckBox(false);
                    }
                    bar_right.setText("编辑");
                    requestData();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isLoad=false;
    }
}
